package mailgod;

import processing.core.*;
public class SpiralObject{
	
	private PApplet parent;
	  String s;
	  String[] splits;
	  PVector[] pointsInInSpace;
	  PVector[] pointsOutInSpace;
	  PVector[] pointsOutInPlane;
	  PVector[] pointsInInPlane;
	  int[] setID;
	  int[] shapeID;
	  float[] currentSpaceDist;
	  float[] currentPlaneDist;
	  float totalSpaceDist;
	  float totalPlaneDist;
	  PFont font;
	  float margin = 0f;
	  int pointCount;

	  public SpiralObject(PApplet p) {
		  parent = p;
	  }

	  public SpiralObject(PApplet p, String theS) {
		  parent = p;
	    s = theS;
	    font = SpiralRender.myFont;
	    parent.textFont(font,42);
	    pointCount = 0;
	  }

	  public void addPoint(PointLayer[] pl, int i, int j, int k) {
	    if (shapeID == null) {
	      pointsInInSpace = new PVector[1];
	      pointsOutInSpace = new PVector[1];
	      pointsInInPlane = new PVector[1];
	      pointsOutInPlane = new PVector[1];
	      setID = new int[1];
	      shapeID = new int[1];
	      currentPlaneDist = new float[1];
	      currentSpaceDist = new float[1];
	      currentPlaneDist[0] = 0f;
	      currentSpaceDist[0] = 0f;
	      pointsInInSpace[0] = pl[k].pointsInInSpace[i];
	      pointsOutInSpace[0] = pl[k+1].pointsOutInSpace[j];
	      pointsInInPlane[0] = pl[k].pointsInInPlane[i];
	      pointsOutInPlane[0] = pl[k+1].pointsOutInPlane[j];
	      setID[0] = pl[k].setID[i];
	      shapeID[0] = pl[k].shapeID[i];
	    } 
	    pointsInInSpace = (PVector[])PApplet.append(pointsInInSpace,pl[k].pointsInInSpace[i]);
	    pointsOutInSpace = (PVector[])PApplet.append(pointsOutInSpace,pl[k+1].pointsOutInSpace[j]);
	    pointsInInPlane = (PVector[])PApplet.append(pointsInInPlane,pl[k].pointsInInPlane[i]);
	    pointsOutInPlane = (PVector[])PApplet.append(pointsOutInPlane,pl[k+1].pointsOutInPlane[j]);
	    setID = (int[])PApplet.append(setID,pl[k].setID[i]);
	    shapeID = (int[])PApplet.append(shapeID, pl[k].shapeID[i]);
	    //////////////
	    float sDist = pointsInInSpace[shapeID.length-1].dist(pointsOutInSpace[shapeID.length-1]);
	    float pDist = pointsInInPlane[shapeID.length-1].dist(pointsOutInPlane[shapeID.length-1]);
	    currentSpaceDist =(float[])PApplet.append(currentSpaceDist,sDist);
	    currentPlaneDist =(float[])PApplet.append(currentPlaneDist,pDist);
	    totalSpaceDist += sDist;
	    totalPlaneDist += pDist;

	    pointCount++;
	  }

	  public void displayPointsInSpace() {
	    if (pointsInInSpace != null) {
	      parent.beginShape(PApplet.LINES);
	      for (int i = 0; i < pointsInInSpace.length; i++) {
	    	  parent.vertex(pointsInInSpace[i].x,pointsInInSpace[i].y,pointsInInSpace[i].z);
	    	  parent.vertex(pointsOutInSpace[i].x,pointsOutInSpace[i].y,pointsOutInSpace[i].z);
	      }
	      parent.endShape();
	    }
	  }

	  public void displayPointsInPlane(int theSet, int theShape) {
	    if (pointsInInPlane != null) {

	      for (int i = 0; i < pointsInInPlane.length; i++) {
	                    if (setID[i] == theSet && shapeID[i] == theShape) {
	          parent.beginShape(PApplet.LINES);
	        parent.vertex(pointsInInPlane[i].x,pointsInInPlane[i].y,0);
	        parent.vertex(pointsOutInPlane[i].x,pointsOutInPlane[i].y,0);
	      }
	      parent.endShape();
	            }
	    }
	  }

	  public void splitTextInSpace () {
	    splits = new String[pointCount];
	    int charCount = 0;
	    for (int i = 0; i < pointCount; i++) {
	      splits[i] = "";
	      while(parent.textWidth(splits[i]) <= currentSpaceDist[i] && charCount < s.length() ) {
	        splits[i] += s.charAt(charCount);
	        charCount++;
	      }
	      charCount--;
	    }
	  }

	  void splitTextInPlane () {
		    while(parent.textWidth(s) > totalPlaneDist*1.5f ) {
		      s = s.substring(1,s.length()-1);
		    }
		    //println(s.length());
		    if (s != null) {
		    splits = new String[pointCount];
		    int charCount = 0;
		    for (int i = 0; i < pointCount; i++) {
		      splits[i] = "";
		      while((parent.textWidth(splits[i])-2*margin) <= currentPlaneDist[i] && charCount < s.length() ) {
		        splits[i] = s.charAt(charCount)+splits[i];
		        charCount++;
		      }
		     // charCount--;
		    }
		    }
		  }
	  public void displayTextInSpace () {

	    PVector x = new PVector(1,0,0);
	    PVector v;
	    PVector v1;
	    PVector v2;

	    float yr;
	    float zr;
	    for (int i = 0; i < pointCount; i++) {
	      v  = new PVector ((pointsOutInSpace[i].x-pointsInInSpace[i].x),(pointsOutInSpace[i].y-pointsInInSpace[i].y),(pointsOutInSpace[i].z-pointsInInSpace[i].z));
	      v1 = new PVector(v.x,0,v.z);
	      v2 = new PVector(v.x,v.y,0);
	      v1.normalize();
	      v2.normalize();

	      yr = PVector.angleBetween(x,v1);
	      zr = PVector.angleBetween(x,v2);
	      parent.pushMatrix();
	      parent.translate(pointsInInSpace[i].x,pointsInInSpace[i].y,pointsInInSpace[i].z);
	      //if (yr == HALF_PI) yr += PI;
	      //if (zr > PI) zr = zr-PI;
	      parent.rotateY(yr);
	      //rotateZ(zr);


	      parent.rotateX(-PApplet.PI);


	      parent.text(splits[i],0,0,0);
	      parent.popMatrix();
	      //  println("point nr. " + i + " : " + degrees(yr));
	    }
	  }

	  void displayTextInPlane (int theSet, int theShape) {
		    float r;
		    for (int i = 0; i < pointCount; i++) {
		      if (setID[i] == theSet && shapeID[i] == theShape) {
		        r = PApplet.atan((pointsInInPlane[i].y-pointsOutInPlane[i].y)/(pointsInInPlane[i].x-pointsOutInPlane[i].x));
		        parent.pushMatrix();
		        parent.translate(pointsInInPlane[i].x,pointsInInPlane[i].y);
		        parent.rotate(r); ///or "-r" ?
		        parent.textAlign(PApplet.RIGHT);
		        parent.fill(255);
		        parent.text(splits[i],+margin,0);
		        //parent.filter(PApplet.BLUR,7);
		        parent.fill(255, 225, 35);
		        parent.text(splits[i],+margin,0);
		        //parent.filter(parent.BLUR,4);
		        parent.fill(255);
		        parent.text(splits[i],+margin,0);
		        parent.popMatrix();
		      }
		    }
		  }


	  public void clearPoints() {
	    pointsInInSpace = null;
	    pointsOutInSpace = null;
	    pointsInInPlane = null;
	    pointsOutInPlane = null;

	    setID = null;
	    shapeID = null;
	    currentSpaceDist = null;
	    currentPlaneDist = null;
	    totalSpaceDist = 0f;
	    totalPlaneDist = 0f;
	    pointCount = 0;
	  }
	}


